#include "dataprepare.h"
#include "CTAsyncCmd.h"
#include "../mainctrl.h"

#include "cmdselectdatafromdbsvr.h"
#include "../cmdinner/CCmdGetPlayerOnlineData.h"

CDataPrepare::CDataPrepare()
{

}

CDataPrepare::~CDataPrepare()
{

}

int CDataPrepare::GetPlayerBaseInfo(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex >= 0)
    {
        return 0;
    }
    
    iRet = GetPlayerBaseInfoFromDB(pstAsyncCmd, uiGID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

int CDataPrepare::GetPlayerBaseInfoFromDB(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    //从数据库获取
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(ID_MAINDB_SELECTDATA);
    if (!pstCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq.m_ucCheckUpdateMgr = 1;
    pstCmd->stReq.m_stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    pstCmd->stReq.m_stKey.m_u64ID = uiGID;

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstCmd, true);
    }

    int iRet = pstCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdSelectDataFromDBSvr do() error"));
        return RET_SYS_ERROR;
    }
    return 0;
}

bool CDataPrepare::IsPlayerBaseInfoPrepared(unsigned int uiGID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerBaseInfoHash.HashFind(uiGID);
    return iIndex >= 0;
}

//////////////////////////////////////////////////////////////////////////

int CDataPrepare::GetPlayerDetailInfo(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerDetailInfoHash.HashFind(uiGID);
    if (iIndex >= 0)
    {
        return 0;
    }

    iRet = GetPlayerDetailInfoFromDB(pstAsyncCmd, uiGID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

int CDataPrepare::GetPlayerDetailInfoFromDB(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    //从数据库获取
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(ID_MAINDB_SELECTDATA);
    if (!pstCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq.m_ucCheckUpdateMgr = 1;
    pstCmd->stReq.m_stKey.m_chDataType = DT_PLAYER_DETAIL_INFO;
    pstCmd->stReq.m_stKey.m_u64ID = uiGID;

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstCmd, true);
    }

    int iRet = pstCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdSelectDataFromDBSvr do() error"));
        return RET_SYS_ERROR;
    }
    return 0;
}

bool CDataPrepare::IsPlayerDetailInfoPrepared(unsigned int uiGID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerDetailInfoHash.HashFind(uiGID);
    return iIndex >= 0;
}

//////////////////////////////////////////////////////////////////////////

int CDataPrepare::GetPlayerDetailInfoEx1 (CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerDetailEx1InfoHash.HashFind(uiGID);
    if (iIndex >= 0)
    {
        return 0;
    }

    iRet = GetPlayerDetailInfoEx1FromDB(pstAsyncCmd, uiGID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

int CDataPrepare::GetPlayerDetailInfoEx1FromDB(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    //从数据库获取
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(ID_MAINDB_SELECTDATA);
    if (!pstCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq.m_ucCheckUpdateMgr = 1;
    pstCmd->stReq.m_stKey.m_chDataType = DT_PLAYER_DETAIL_EX1_INFO;
    pstCmd->stReq.m_stKey.m_u64ID = CT_UINT64_MAKE(0, uiGID);

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstCmd, true);
    }

    int iRet = pstCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdSelectDataFromDBSvr do() error"));
        return RET_SYS_ERROR;
    }
    return 0;
}

bool CDataPrepare::IsPlayerDetailInfoEx1Prepared(unsigned int uiGID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerDetailEx1InfoHash.HashFind(uiGID);
    return iIndex >= 0;
}

//////////////////////////////////////////////////////////////////////////

int CDataPrepare::GetPlayerSnsDetailInfo(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerSnsDetailInfoHash.HashFind(uiGID);
    if (iIndex >= 0)
    {
        return 0;
    }

    iRet = GetPlayerSnsDetailInfoFromDB(pstAsyncCmd, uiGID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

int CDataPrepare::GetPlayerSnsDetailInfoFromDB(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    //从数据库获取
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(ID_MAINDB_SELECTDATA);
    if (!pstCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq.m_ucCheckUpdateMgr = 1;
    pstCmd->stReq.m_stKey.m_chDataType = DT_PLAYER_SNS_DETAIL_INFO;
    pstCmd->stReq.m_stKey.m_u64ID = uiGID;

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstCmd, true);
    }

    int iRet = pstCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdSelectDataFromDBSvr do() error"));
        return RET_SYS_ERROR;
    }
    return 0;
}

bool CDataPrepare::IsPlayerSnsDetailInfoPrepared(unsigned int uiGID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerSnsDetailInfoHash.HashFind(uiGID);
    return iIndex >= 0;
}

//////////////////////////////////////////////////////////////////////////

int CDataPrepare::GetUnactPartnerInfo(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stUnactPartnerInfoHash.HashFind(uiGID);
    if (iIndex >= 0)
    {
        return 0;
    }

    iRet = GetUnactPartnerInfoFromDB(pstAsyncCmd, uiGID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

int CDataPrepare::GetUnactPartnerInfoFromDB(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    //从数据库获取
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(ID_MAINDB_SELECTDATA);
    if (!pstCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq.m_ucCheckUpdateMgr = 1;
    pstCmd->stReq.m_stKey.m_chDataType = DT_PLAYER_UNACT_PARTNER;
    pstCmd->stReq.m_stKey.m_u64ID = uiGID;

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstCmd, true);
    }

    int iRet = pstCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdSelectDataFromDBSvr do() error"));
        return RET_SYS_ERROR;
    }
    return 0;
}

bool CDataPrepare::IsUnactPartnerInfoPrepared(unsigned int uiGID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stUnactPartnerInfoHash.HashFind(uiGID);
    return iIndex >= 0;
}

//////////////////////////////////////////////////////////////////////////
int CDataPrepare::GetInstanceZoneDetailInfo(CTAsyncCmd* pstAsyncCmd, int iID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stInstanceZoneDetailInfoHash.HashFind(iID);
    if (iIndex >= 0)
    {
        return 0;
    }

    iRet = GetInstanceZoneDetailInfoFromDB(pstAsyncCmd, iID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

bool CDataPrepare::IsInstanceZoneDetailInfoPrepared(int iID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stInstanceZoneDetailInfoHash.HashFind(iID);
    return iIndex >= 0;
}

int CDataPrepare::GetInstanceZoneDetailInfoFromDB(CTAsyncCmd* pstAsyncCmd, int iID)
{
    //从数据库获取
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(ID_MAINDB_SELECTDATA);
    if (!pstCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stReq.m_ucCheckUpdateMgr = 1;
    pstCmd->stReq.m_stKey.m_chDataType = DT_INSTANCE_ZONE_DETAIL_INFO;
    pstCmd->stReq.m_stKey.m_u64ID = iID;

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstCmd, true);
    }

    int iRet = pstCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdSelectDataFromDBSvr do() error"));
        return RET_SYS_ERROR;
    }
    return 0;
}

//////////////////////////////////////////////////////////////////////////
int CDataPrepare::GetPlayerOnlineData(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    int iRet = 0;

    //检查hash中是否存在
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex >= 0)
    {
        return 0;
    }

    iRet = GetPlayerOnlineDataFromDB(pstAsyncCmd, uiGID);
    if (iRet != 0)
    {
        return iRet;
    }

    return RET_DATA_NOT_READY;
}

bool CDataPrepare::IsPlayerOnlineDataPrepared(unsigned int uiGID)
{
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerOnlineDataHash.HashFind(uiGID);
    return iIndex >= 0;
}

int CDataPrepare::GetPlayerOnlineDataFromDB(CTAsyncCmd* pstAsyncCmd, unsigned int uiGID)
{
    CCmdGetPlayerOnlineData* pstNewCmd = (CCmdGetPlayerOnlineData*)CT_CMDFACTORY->CreateCmd(ID_MAIN_INNER_GET_PLAYER_ONLINE_DATA);
    if (!pstNewCmd)
    {
        CT_WARNING(("create cmd error"));
        if (pstAsyncCmd != NULL)
        {
            return pstAsyncCmd->LogicDo(RET_SYS_ERROR);
        }
        else
        {
            return RET_SYS_ERROR;
        }
    }

    pstNewCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstNewCmd->uiGID = uiGID;

    if (pstAsyncCmd != NULL)
    {
        pstAsyncCmd->RegisterChild(pstNewCmd, true);
    }

    int iRet = pstNewCmd->Do(NULL);
    if (iRet != 0)
    {
        CT_WARNING(("CCmdGetPlayerOnlineData do() error"));
        return RET_SYS_ERROR;
    }

    return 0;
}

